<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
/*
 *  @des    sms短信类、短信接口
 *  @author zhu
 *  
 */
class Sms{

    private $url="http://sms.10690221.com:9011/";//接口url
	private $company="sxhm";//企业代码
	private $uid="805791";//用户ID
	private $password="hm123456";//密码
	private $expid="0";//扩展码
	private $msg="";
	private $phone="";
	private $encode="utf-8";
	private $time="";

	protected $CI;

	public function __construct(){
		$this->CI=&get_instance();
	}

	public function testt(){
		$data=array("dd","cc");
		$data['data']=$this->moneySMS();
		return $data;
	}

	//短信入口
	public function send($user,$url,$phone,$rand){
		$data=array();
		$logdata=array();
		$this->CI->load->model("hospital");
		$this->CI->load->model("hospitalurl");

		$urlinfo=$this->CI->hospitalurl->checkurl($url);
		$hospitalinfo=$this->CI->hospital->getinfoid($urlinfo['yiyuanid']);
		
		$this->CI->load->helper('func');
		$week=returnweek();
		$time=date("Y-m-d",time())."+".$week;

		$data['mobile']=$phone;
		$data['msg']="【".$hospitalinfo['topname']."】".$user."您好，您已成功预约".$time.",".$hospitalinfo['name']."的预约号，订单号：".$rand."+医院电话：".$hospitalinfo['phone'];

		$reg=$this->sendSMS($data);//发送短信

		$logdata['mobile']=$data['mobile'];
		$logdata['msg']=$data['msg'];
		$logdata['data']=$reg['data'];
		$this->logSMS($logdata);//写入日志
		return true;
	}

	//发送短信
	public function sendSMS($data){
		$msg=array();
		if(!isset($data['mobile'])){
			$msg['msg']="手机号码不能为空";
			$msg['result']=false;
			return $msg;
		}

		if(!isset($data['msg'])){
			$msg['msg']="发送消息不能为空";
			$msg['result']=false;
			return $msg;
		}

		$uid=$this->uid;
		$auth=$this->decodeSMS();
		$expid=$this->expid;
		$encode=$this->encode;
		$url=$this->url."hy/?uid=".$uid."&auth=".$auth."&mobile=".$data['mobile']."&msg=".$data['msg']."&expid=".$expid."&encode=".$encode;

		$result=$this->curlSMS($url);

		$res=explode(",",$result);		
		if($res[0]=="0"){
			$msg['msg']="send success";
			$msg['result']=true;
			$msg['data']=$result;
		}else{
			$msg['msg']="send fail";
			$msg['result']=false;
			$msg['data']=$result;
		}
		
		return $msg;
	}

	//余额查询接口
	public function moneySMS(){

		$uid=$this->uid;
		$auth=MD5($this->company.$this->password);
		$url=$this->url."hy/m?uid=".$uid."&auth=".$auth;

		return $this->curlSMS($url);
	}

	//验证码编译
	private function decodeSMS(){
		return md5($this->company.$this->password);
	}

	//curl响应
	private	function curlSMS($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result; 
	}

	//写入日志
	public function logSMS($data){
		$this->CI->load->model('smslog');

		$state=explode(",",$data['data']);
		$data['smsstate']=$state[0];
		$data['smsid']=$state[1];

		$insert_id=$this->CI->smslog->insertdata($data);
		return $insert_id;
	}

}
?>